package com.qk.mangement.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.mangement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {






    @Autowired
    private UserService userService;



    // 根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        return Result.success(userService.listByRole(roleLabel));
    }

    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids) {
        log.info("删除用户:{}",ids);
        userService.deleteById(ids);
        return Result.success();
    }



    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户:{}",user);
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 条件分页查询用户列表
     */
    @GetMapping
    public Result list(UserDTO dto) {

        PageResult<UserDO> userPage = userService.list(dto);
        return Result.success(userPage);
    }
}